<template>
  <div>
    <van-nav-bar title="编辑地址" left-arrow placeholder fixed @click-left="GOback" @click-right="Switch" :border="false">
      <template #right>
        <div class="rightText">{{rightText}}</div>
      </template>
    </van-nav-bar>
    <ul class="addresslist">
      <li v-for="item in ShippingAddress" :key="item.id">
        <div>
          <p>{{item.address}}</p>
          <p>{{item.phone}}</p>
        </div>
        <span v-if="show" @click="DeleteID(item.id)">x</span>
      </li>

    </ul>
    <div class="addsite" @click="newaddress">
      <span>新增地址</span>
      <van-icon name="arrow" size="0.8rem" color="#ddd" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      rightText: '编辑',
      show: false
    }
  },
  computed: {
    ...mapState(['ShippingAddress'])
  },
  methods: {
    GOback() {
      this.$router.go(-1)
    },
    // 获取收货地址信息
    getAddressData() {
      this.$store.dispatch('getAddress', window.sessionStorage.getItem('user_id'))
    },
    // 点击新增地址跳转到新增地址
    newaddress() {
      this.$router.push('/user/info/address/add')
    },
    // 点击编辑显示删除按钮并将编辑文字 改为完成
    Switch() {
      if (this.rightText === '编辑' && this.show === false) {
        this.rightText = '完成'
        this.show = true
      } else {
        this.rightText = '编辑'
        this.show = false
      }
    },
    // 删除收货地址
    async DeleteID(item_id) {
      const id = window.sessionStorage.getItem('user_id')
      const { data: res } = await this.reques.delete(`v1/users/${id}/addresses/${item_id}`)
      if (res.success === '删除地址成功') {
        this.getAddressData()
      }
    }
  },
  mounted() {
    this.getAddressData()
  }
}
</script>

<style lang="less" scoped>
.addresslist {
  margin-top: 0.4rem;
  border-top: 1px solid #d9d9d9;
  background: #fff;
  li {
    border-bottom: 1px solid #d9d9d9;
    padding: 0.4rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    p {
      line-height: 0.9rem;
      font-size: 0.55rem;
      color: #333;
    }
    span {
      font-size: 0.7rem;
      color: #999;
    }
  }
}
.addsite {
  margin-top: 0.4rem;
  background: #fff;
  padding: 0.2rem 0.4rem;
  border-bottom: 1px solid #d9d9d9;
  border-top: 1px solid #d9d9d9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  span {
    display: block;
    font-size: 0.6rem;
    color: #333;
    line-height: 1.4rem;
  }
}
.rightText {
  color: #fff;
}
</style>